import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:grint/page/discovery/challenge_page.dart';
import 'package:grint/page/discovery/record_page.dart';
import 'package:grint/page/discovery/share_page.dart';
import 'package:grint/page/home/guide_page.dart';
import 'package:grint/page/home/science_page.dart';
import 'package:grint/page/home/video_detail_page.dart';
import 'package:grint/page/message/chat_page.dart';

import '../page/home/home_page.dart';
import '../page/discovery/discovery_page.dart';
import '../page/profile/profile_page.dart';
import '../page/profile/others_page.dart';
import '../page/message/message_page.dart';
import '../page/upload/create_page.dart';
import '../page/profile/settings_page.dart';
import '../page/main/root_layout.dart';
import '../page/main/splash_page.dart';
import '../page/main/auth_page.dart';
import '../enum/app_routes.dart';

class AppRouter {
  static final GlobalKey<NavigatorState> rootNavigatorKey =
      GlobalKey<NavigatorState>();
  static final GlobalKey<NavigatorState> shellNavigatorKey =
      GlobalKey<NavigatorState>();

  static final GoRouter router = GoRouter(
    initialLocation: '/splash',
    navigatorKey: rootNavigatorKey,
    routes: [
      GoRoute(
        path: '/splash',
        pageBuilder:
            (context, state) =>
                NoTransitionPage(key: state.pageKey, child: const SplashPage()),
      ),

      GoRoute(
        path: '/auth',
        parentNavigatorKey: rootNavigatorKey,
        pageBuilder:
            (context, state) =>
                NoTransitionPage(key: state.pageKey, child: const AuthPage()),
      ),

      ShellRoute(
        navigatorKey: shellNavigatorKey,
        builder: (context, state, child) {
          return RootLayout(location: state.uri.toString(), child: child);
        },
        routes: [
          GoRoute(
            path: AppRoutes.home,
            pageBuilder:
                (context, state) => NoTransitionPage(
                  key: state.pageKey,
                  child: const HomePage(),
                ),
          ),

          GoRoute(
            path: AppRoutes.discovery,
            pageBuilder:
                (context, state) => NoTransitionPage(
                  key: state.pageKey,
                  child: const DiscoveryPage(),
                ),
          ),

          GoRoute(
            path: AppRoutes.message,
            pageBuilder:
                (context, state) => NoTransitionPage(
                  key: state.pageKey,
                  child: const MessagePage(),
                ),
          ),

          GoRoute(
            path: AppRoutes.profile,
            pageBuilder:
                (context, state) => NoTransitionPage(
                  key: state.pageKey,
                  child: const ProfilePage(),
                ),
          ),
        ],
      ),

      GoRoute(
        path: AppRoutes.create,
        parentNavigatorKey: rootNavigatorKey,
        pageBuilder: (context, state) {
          final extra = state.extra as Map<String, dynamic>?;
          final uploadType =
              extra?['uploadType'] as ContentUploadType? ??
              ContentUploadType.dailyShare;
          final challengeTask = extra?['challengeTask'] as String?;

          return NoTransitionPage(
            key: state.pageKey,
            child: CreatePage(
              uploadType: uploadType,
              preselectedChallenge: challengeTask,
            ),
          );
        },
      ),

      GoRoute(
        path: '/discovery/record',
        parentNavigatorKey: rootNavigatorKey,
        pageBuilder:
            (context, state) =>
                NoTransitionPage(key: state.pageKey, child: const RecordPage()),
      ),

      GoRoute(
        path: '/profile/settings',
        parentNavigatorKey: rootNavigatorKey,
        pageBuilder:
            (context, state) => NoTransitionPage(
              key: state.pageKey,
              child: const SettingsPage(),
            ),
      ),

      GoRoute(
        path: '/home/science',
        parentNavigatorKey: rootNavigatorKey,
        pageBuilder:
            (context, state) => NoTransitionPage(
              key: state.pageKey,
              child: const SciencePage(),
            ),
      ),

      GoRoute(
        path: '/home/video-detail',
        parentNavigatorKey: rootNavigatorKey,
        pageBuilder: (context, state) {
          final extra = state.extra as Map<String, dynamic>?;
          return NoTransitionPage(
            key: state.pageKey,
            child: VideoDetailPage(
              videoPath: extra?['videoPath'] ?? '',
              videoContent: extra?['videoContent'] ?? '',
              coverImage: extra?['coverImage'] ?? '',
              viewCount: extra?['viewCount'] ?? 0,
              videoId: extra?['videoId'],
            ),
          );
        },
      ),

      GoRoute(
        path: '/home/guide',
        parentNavigatorKey: rootNavigatorKey,
        pageBuilder:
            (context, state) =>
                NoTransitionPage(key: state.pageKey, child: const GuidePage()),
      ),

      GoRoute(
        path: '/message/chat',
        parentNavigatorKey: rootNavigatorKey,
        pageBuilder:
            (context, state) =>
                NoTransitionPage(key: state.pageKey, child: const ChatPage()),
      ),

      GoRoute(
        path: '/discovery/challenge',
        parentNavigatorKey: rootNavigatorKey,
        pageBuilder:
            (context, state) => NoTransitionPage(
              key: state.pageKey,
              child: const ChallengePage(),
            ),
      ),

      GoRoute(
        path: '/profile/others/:userId',
        parentNavigatorKey: rootNavigatorKey,
        pageBuilder: (context, state) {
          final userId = state.pathParameters['userId'] ?? '';

          if (userId.isEmpty) {
            return NoTransitionPage(
              key: state.pageKey,
              child: Scaffold(
                appBar: AppBar(title: const Text('User Not Found')),
                body: const Center(child: Text('User ID not provided')),
              ),
            );
          }

          return NoTransitionPage(
            key: state.pageKey,
            child: OthersPage(userId: userId),
          );
        },
      ),

      GoRoute(
        path: '/discovery/share/:shareId',
        parentNavigatorKey: rootNavigatorKey,
        pageBuilder:
            (context, state) => NoTransitionPage(
              key: state.pageKey,
              child: SharePage(shareId: state.pathParameters['shareId'] ?? ''),
            ),
      ),
    ],
  );
}
